package com.caigou.modules.forum.ui.publish

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.modules.forum.adpter.ForumPageAdapter
import com.caigou.modules.forum.databinding.ActivityPublishContestBinding
import com.caigou.modules.forum.ui.publish.fragment.PingMaFragment
import com.caigou.modules.forum.ui.publish.fragment.TeMaFragment
import com.caigou.modules.forum.ui.publish.fragment.TeXiaoFragment
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/25
 */
@Route(path = RouterPath.Forum.PAGE_PUBLISH_CONTEST)
@AndroidEntryPoint
class PublishContestActivity : BaseActivity() {

    private lateinit var binding: ActivityPublishContestBinding

    @JvmField
    @Autowired(name = AppConstants.FORUM_TYPE)
    var forumType = 1

    @JvmField
    @Autowired(name = AppConstants.DARE_TYPE)
    var dareType = 0

    private val itemNames = mutableListOf(
        "特肖",
        "特码",
        "平码"
    )

    private val fragments = mutableListOf<Fragment>(
        TeXiaoFragment(),
        TeMaFragment(),
        PingMaFragment()
    )

    private val pagerAdapter: ForumPageAdapter by lazy {
        ForumPageAdapter(this)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPublishContestBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.publishContestLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        pagerAdapter.setList(fragments)
        binding.publishContestViewPager2.adapter = pagerAdapter
        binding.publishContestViewPager2.isUserInputEnabled = false
        TabLayoutMediator(binding.publishContestTabLayout, binding.publishContestViewPager2, true, true) { tab, position ->
            tab.text = itemNames[position]
        }.attach()
        binding.publishContestViewPager2.offscreenPageLimit = itemNames.size
        binding.publishContestViewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.publishContestTvTitle.text = when (position) {
                    0 -> "参数特肖"
                    1 -> "参数特码"
                    2 -> "参数平码"
                    else -> ""
                }
            }
        })
        binding.publishContestViewPager2.currentItem = dareType - 1
    }


}